#ifndef __LAYERMANAGEREX_H__
#define __LAYERMANAGEREX_H__

#include "..\LayerManager\LayerManager.h"

#define MaxNumGroup 32
#define MaxNumInvReg 32

struct LAYERGROUP
{
    LAYERGROUP(int width, int height);

    LayerManager        lm;
    Point               Position;
    Region              ClippingRegion;
    int                 bEnable;
    LAYERGROUP*         pPrevGroup;
    LAYERGROUP*         pNextGroup;
    int                 nIndex;
};

class LayerManagerEX
{
public:
    LayerManagerEX(int width, int height);
    ~LayerManagerEX();

    void RenderToBuffer(BitmapBufInfo & img, int OffsetX, int OffsetY,
                        int RenderLen,
                        int RenderHt);

    int CreateNewGroup();

    int GroupBringForward(int index);
    int GroupSendBackward(int index);
    int GroupBringToFront(int index);
    int GroupSendToBack(int index);

    int RemoveGroupByIndex(int index);
    void RemoveAllGroups();

    void EnableAllGroups();
    void DisableAllGroups();
    void EnableGroupByIndex(int index);
    void DisableGroupByIndex(int index);

    void SelectGroup(int index);
// Original LayerManager Interface
//
    int AddLayer(BitmapBufInfo & img, int x=0, int y=0, int level=255);

    int  LayerBringForward(int index);
    int  LayerSendBackward(int index);
    int  LayerBringToFront(int index);
    int  LayerSendToBack(int index);
    void LayerRestore(int index);
    void LayerFlip(int index, int direction);
    int  RemoveLayerByIndex(int index);
    void RemoveAllLayers();
    int  DuplicateLayer(int index, int xin, int yin, int level, int style);
    int  LayerResize(int index, int width, int height);
    int  LayerRotate90(int index, int times);
    void SetLayerTransparency(int index, int level);
    int  GetLayerTransparency(int index);
    void ShowLayer(int index, bool visible);
    void MoveLayer(int index, int x, int y);
    void SetLayerPosition(int index, int x, int y);
    void SetLayerBlendType(int index, int type);
    void SetLayerColor(int index, unsigned char R, unsigned char G, unsigned char B);
    int  GetLayerType(int index);
    void SetBackgroundColor(unsigned char R, unsigned char G, unsigned char B);
    void StretchLayer(int index, int StretchX, int StretchY);
    void ResizeLayer(int index, int StretchX, int StretchY, int alignX, int alignY);
    int  WarpLayerLR(int index, int StretchX, int StretchYL, int StretchYR);
    int  WarpLayerTD(int index, int StretchY, int StretchXT, int StretchXD);
    int  ShearLayer(int index, int offset, int StretchX, int StretchY);
    int  WaveLayer(int index, int* aSampleX16, int width, int height, int InnerWidth);
    int  MergeLayer(int SrcIndex, int DstIndex);
    void SetViewPosition(int x, int y);
    void IsTransparent(bool flag);
    void ZoomLayer(int index, float zoomlevel);
    void ZoomLayerByPoint(int index, float zoomlevel, int x, int y);
// End
//

    Point*    GetLayerPosition(int index);
    BitmapBufInfo*    GetLayerBits(int index);
    Layer*    GetLayerInfo(int index);
    BitmapBufInfo*    GetSrcLayerBits(int index);

    void SetClippingRegion(int tx, int ty, int bx, int by);
    void SetScreenSize(int width, int height);
    void ClearClippingRegion();
    void ForceLayerInvalidRegion(int index);

    int GetNumberOfGroups();
    int GetAllLayerIds(int* pid);
    int GetNumberOfLayers();

    LayerManager* GetGroupLayerManager(int gid);

    int  GetInvalidRegionNum();
    void GetInvalidRegion(int id, int* tx, int* ty, int* bx, int* by);

    int m_nScreenWidth;
    int m_nScreenHeight;

private:

    bool MergeIntersection(int tx, int ty, int bx, int by, int id);
    void AddInvalidRegion(int tx, int ty, int bx, int by);
    void AddInvalidRegion(Region invreg);

    LAYERGROUP*                     m_pGroup[MaxNumGroup];
    LAYERGROUP*                     m_pTopGroup;
    LAYERGROUP*                     m_pBottomGroup;
    Region                          m_aInvalidRegion[MaxNumInvReg];
    int                             m_nNumberOfGroups;
    int                             m_pAvailableSlot;
    int                             m_nNumberOfInvRegions;
    int                             m_nGid;
    LAYERGROUP                      m_stDefaultGroup;
};

#endif // __LAYERMANAGEREX_H__



